X_AXIS, Y_AXIS = 1, 2

def setup
  size 200, 200
  
  b1 = color 190, 190, 190
  b2 = color 20, 20, 20
  c1 = color 255, 120, 0
  c2 = color 10, 45, 255
  c3 = color 10, 255, 15
  c4 = color 125, 2, 140
  c5 = color 255, 255, 0
  c6 = color 25, 255, 200
  
  [
    [0, 0, width, height, b1, b2, Y_AXIS],
    [25, 25, 75, 75, c1, c2, Y_AXIS],
    [100, 25, 75, 75, c3, c4, X_AXIS],
    [25, 100, 75, 75, c2, c5, X_AXIS],
    [100, 100, 75, 75, c4, c6, Y_AXIS]
  ].each {|args| set_gradient *args}
end

def set_gradient(x, y, w, h, c1, c2, axis)
  r, g, b = red(c1), green(c1), blue(c1)
  delta_r = red(c2) - r
  delta_g = green(c2) - g
  delta_b = blue(c2) - b

  case axis
  when Y_AXIS
    y.upto(y + h) do |n|
      coef = (n - y) / h.to_f
      c = color r + delta_r * coef,
                g + delta_g * coef,
                b + delta_b * coef
      stroke c
      line x, n, x + w, n
    end
  when X_AXIS
    x.upto(x + w) do |n|
      coef = (n - x) / w.to_f
      c = color r + delta_r * coef,
                g + delta_g * coef,
                b + delta_b * coef
      stroke c
      line n, y, n, y + h
    end
  end
end
